# !/usr/bin python3                                
# encoding: utf-8 -*-
# @file: cloud_1190_test.py
# @author   : wjd
# @Time: 2023/12/25 16:32
import decimal
import json

from antalpha_course.Antalpha_test.tools_api.count_method import Count
from datetime import datetime


class Cloud_1190():
    # @pytest.mark.parametrize(1, [0.001, 0.5], 118799)
    def lilvjisuan(self, a: list, b: list, c: list, d: list, e: list, f: list):
        """
        多段利率计算
        表：invest_pledge_loan_order_operation_journal
        :param a: 借款金额
        :param b:年华利率
        :param c:借款秒数 （结束毫秒时间-开始毫秒时间）/1000转换成秒+1秒 (1722441599000-1721736000000)/1000+1
        :param n: 列表长度
        根据借款利率来分割成两段，然后以当前借款金额为基础，算出对应时间段内 借款金额*借款利率*借款秒数
        公式：借款金额（首次放款（线上、线下）+追加放款(线上、线下)+未还月利息+未还其他收费）*多段利息*借款秒数
        :return:
        """
        # (0.001*0.99999999/31536000*433240)+(0.001*0.5/31536000*118799）
        sum_jie = 0
        sum_huan = 0
        print(
            "***********************************************【借款结算】********************************************************")
        for i in range(len(a)):
            print("第{}次计算==================>>>".format(i + 1))
            # a = str(borrow)
            count = Count()
            rate = count.chufa_32(b[i], 31536000)
            sum1 = (decimal.Decimal(str(a[i])) * decimal.Decimal(rate) * decimal.Decimal(c[i]))
            sum_jie += sum1
            print("借款是：{}".format(a[i]))
            print("32位利率是:{}".format(rate))  # 算出利率
            print("借款秒数是：{}".format(c[i]))
            print("单次科学计数结果是：{}".format(sum1))
            print("--" * 50)

        a_jie = str(sum_jie).split('.')[0] + '.' + str(sum_jie).split('.')[1][:8]
        b_jie = str(sum_jie).split('.')[0] + '.' + str(sum_jie).split('.')[1][:10]
        print("最终结果8 位是 ======>>>【{}】".format(a_jie))
        print("最终结果10位是 ======>>>【{}】".format(b_jie))
        print("--" * 50)
        print(
            "***********************************************【部分还款计算】******************************************************")
        for i in range(len(d)):
            print("部分提前还款 第{}次计算==================>>>".format(i + 1))
            count = Count()
            rate = count.chufa_32(e[i], 31536000)
            sum1 = (decimal.Decimal(str(d[i])) * decimal.Decimal(rate) * decimal.Decimal(f[i]))
            sum_huan += sum1
            print("部分提前还款 借款是：{}".format(d[i]))
            print("部分提前还款 32位利率是:{}".format(rate))  # 算出利率
            print("部分提前还款 借款秒数是：{}".format(f[i]))
            print("部分提前还款 单次科学计数结果是：{}".format(sum1))
            print("--" * 50)
        a_huan = str(sum_huan).split('.')[0] + '.' + str(sum_huan).split('.')[1][:8]
        b_huan = str(sum_huan).split('.')[0] + '.' + str(sum_huan).split('.')[1][:10]
        print("部分提前还款 最终结果8 位是 ======>>>【{}】".format(a_huan))
        print("部分提前还款 最终结果10位是 ======>>>【{}】".format(b_huan))
        print(
            "***********************************************【计算最终结果】********************************************************")
        sum_result = sum_jie - sum_huan
        a_result = str(sum_result).split('.')[0] + '.' + str(sum_result).split('.')[1][:8]
        b_result = str(sum_result).split('.')[0] + '.' + str(sum_result).split('.')[1][:10]
        print("计算最终结果8 位是 ======>>>【{}】".format(a_result))
        print("计算最终结果10位是 ======>>>【{}】".format(b_result))

    def json_data(self, data):

        # 原始的JSON数据 data
        # 将字符串转换为JSON对象
        interest_rates = data["interestRateRangeExResponses"]
        time_list = []
        loanAmount_list = []
        interestRate_list = []
        time_list_pay = []
        loanAmount_list_pay = []
        interestRate_list_pay = []
        # 遍历每个利率区间
        for rate in interest_rates:
            # 提取 beginTime 和 endTime
            begin_time = rate["beginTime"]
            end_time = rate["endTime"]

            # 将时间戳转换为 datetime 对象
            begin_time_dt = datetime.fromtimestamp(begin_time / 1e3)  # 除以1e9是因为时间戳是以微秒为单位的
            end_time_dt = datetime.fromtimestamp(end_time / 1e3)

            # 打印转换后的时间

            print(f"{rate['operationType']}类型的开始时间: {begin_time_dt.strftime('%Y-%m-%d %H:%M:%S')}")
            print(f"{rate['operationType']}类型的结束时间: {end_time_dt.strftime('%Y-%m-%d %H:%M:%S')}")
            print("\n")
            # 如果你想要打印所有的时间，可以去掉下面这行的注释
            # print("")  # 打印一个空行以便区分不同的利率区间
            # 计算两个时间点之间的时间差
            time_difference = end_time - begin_time

            # 将时间差转换为秒，然后除以 1000，加 1
            result = int((time_difference / 1000) + 1)
            if rate["operationType"] == "PAY_PRINCIPAL":
                time_list_pay.append(result)
                loanAmount_list_pay.append(rate["loanAmount"])
                interestRate_list_pay.append(rate["interestRate"])
            else:
                time_list.append(result)
                loanAmount_list.append(rate["loanAmount"])
                interestRate_list.append(rate["interestRate"])

        # 打印结果
        # print("借款金额:{}".format(loanAmount_list))  # 借款金额
        # print("利率:{}".format(interestRate_list))  # 利率
        # print("\n")
        print("借款金额:{}".format([format(float(i), '.5f') for i in loanAmount_list]))
        print("利率:{}".format([float(i) for i in interestRate_list]))
        print("利率时间:{}".format(time_list))
        print("\n")
        a = [format(float(i), '.5f') for i in loanAmount_list]
        b = [float(i) for i in interestRate_list]
        c = time_list

        # print("部分提前还款 金额:{}".format(loanAmount_list_pay))  # 借款金额
        # print("部分提前还款 利率:{}".format(interestRate_list_pay))  # 利率
        # print("\n")
        print("部分提前还款借款 金额:{}".format([format(float(i), '.5f') for i in loanAmount_list_pay]))
        print("部分提前还款 利率:{}".format([float(i) for i in interestRate_list_pay]))
        print("部分提前还款 利率时间:{}".format(time_list_pay))
        print("\n")
        d = [format(float(i), '.5f') for i in loanAmount_list_pay]
        e = [float(i) for i in interestRate_list_pay]
        f = time_list_pay

        return a, b, c, d, e, f


if __name__ == '__main__':
    data = {
        "principalAmount": "99222.223",
        "principalPending": "false",
        "interestRateRangeExResponses": [{
            "rateId": 13086877524282,
            "endTime": 1717171199000,
            "beginTime": 1714492800000,
            "loanAmount": "102000",
            "interestRate": "0.8888",
            "operationType": "LOAN"
        }, {
            "rateId": 13086877524282,
            "endTime": 1717171199000,
            "beginTime": 1715270400000,
            "loanAmount": "777.777",
            "interestRate": "0.8888",
            "operationType": "PAY_PRINCIPAL"
        }, {
            "rateId": 13086877524282,
            "endTime": 1717171199000,
            "beginTime": 1714492801000,
            "loanAmount": "2200",
            "interestRate": "0.8888",
            "operationType": "PAY_PRINCIPAL"
        }, {
            "rateId": 13086877524282,
            "endTime": 1717171199000,
            "beginTime": 1715914648586,
            "loanAmount": "200",
            "interestRate": "0.8888",
            "operationType": "APPEND_LOAN"
        }]
    }

    cloud1190 = Cloud_1190()
    res = cloud1190.json_data(data)
    print(res)
    Cloud_1190().lilvjisuan(a=res[0], b=res[1], c=res[2], d=res[3], e=res[4], f=res[5])
